package com.edu.hre.db.sql.pim;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreEmpSkill;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.pim.EmpSkillForm;
import com.edu.hre.struts.form.pim.EmpSkillListForm;

public class EmpSkillDAOService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "empSkillDAOService";
	private BaseService baseService  = null;

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	public ArrayList<EmpSkillForm> getEmpSkillList(int limit, int offset, int empNumber){

		ArrayList<EmpSkillForm> listResult = new ArrayList<EmpSkillForm>();

		String sql = "SELECT eski.id AS col1," +
					" ski.name AS col2," +
					" eski.years_of_exp AS col3," +
					" eski.comments AS col4" +

					" FROM hre_emp_skill AS eski" +
					" LEFT OUTER JOIN hre_skill AS ski ON eski.skill_id = ski.id" +
					" LEFT OUTER JOIN hre_employee AS emp ON eski.emp_number = emp.emp_number " +

					" WHERE emp.is_deleted = 0 " +
					" AND emp.emp_number = ? " +
					" ORDER BY eski.date_created DESC";
		
		List <Object[]> listObject = baseService.listArrayObject(sql, limit, offset, empNumber);
		for(int i = 0; listObject != null && i < listObject.size(); i++){

			try {

				Object[] objs = listObject.get(i);
				EmpSkillForm skill = new EmpSkillForm();
	
				skill.setId(Integer.parseInt(CommonUtils.normalizeObjectToString(objs[1], "0")));
				skill.setEmpNumber(empNumber);
				skill.setSkill(CommonUtils.normalizeObjectToString(objs[2], ""));
				skill.setYear(CommonUtils.normalizeObjectToString(objs[3], "0"));
				skill.setComment(CommonUtils.normalizeObjectToString(objs[4], ""));

				listResult.add(skill);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
		
		return listResult;
	}

	public int countEmpSkills(int empNumber){

		int count = baseService.countByColumns(new String[]{"empNumber"}, new int[]{Compare.EQUAL}, new Integer[]{empNumber}, "id", HreEmpSkill.class);
		return count < 0 ? 0 : count;
	}

	public void deleteSkills(EmpSkillListForm form){

		ArrayList<EmpSkillForm> info = (ArrayList<EmpSkillForm>) form.getAllSkills();

		for (int i = 0; i < info.size(); i++) {
			
			try {

				EmpSkillForm work = info.get(i);
				if (work.isSelected()) {
					baseService.deleteFirstPojo("id", Compare.EQUAL, (long) work.getId(), null, null, 0, HreEmpSkill.class);
				}
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
	}
}
